//======================================================================
//    This file is part of baijie_interface
//
//    Copyright (C) 2023 Shenzhen Baijie Technology Co., Ltd.All rights reserved.
//
//    filename : ipset_interface.cpp
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//======================================================================

#include "ipset_interface.h"

void automaticallyGetIp(QStringList ethernetPort)
{
    QString strCmd;
    QString strResult;
    if(!ethernetPort.isEmpty())
    {
        for(int i = 0;i<ethernetPort.size();i++)
        {
            strCmd = QString("dhclient %1").arg(ethernetPort.at(i));
            strResult = executeLinuxCmd(strCmd);
        }
    }
}

bool addStaticIp(QString netName,QString ipAddr,QString mask,QString gate)
{
    QString strCmd = QString("nmcli con show --active|grep static");
    QString strResult = executeLinuxCmd(strCmd);
    QString strEthernetPort = strResult.section(" ",0,1);
    strCmd = QString("nmcli con delete %1").arg(strEthernetPort);
    strResult = executeLinuxCmd(strCmd);
    strCmd = QString("nmcli con add con-name static_ip ifname %1 autoconnect yes type ethernet ip4 %2/%3 gw4 %4") \
            .arg(netName).arg(ipAddr).arg(mask).arg(gate);
    strResult = executeLinuxCmd(strCmd);

    bool result=strResult.contains("successfully added",Qt::CaseInsensitive);
    if(result == true)
    {
        strCmd = QString("nmcli connection up static_ip");
        strResult = executeLinuxCmd(strCmd);
        bool result=strResult.contains("successfully activated",Qt::CaseInsensitive);
        if(result == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}

bool modifyStaticIp(QString ipAddr)
{
    QString gateway;
    for(int i = 9;i < 12;i++)
    {
        if(ipAddr.at(i) == '.')
        {
            gateway = ipAddr.left(i+1);
            gateway.append("1");
            break;
        }
    }
    QString strCmd = QString("nmcli con mod static_ip ipv4.address %2,%3").arg(ipAddr).arg(gateway);
    QString strResult = executeLinuxCmd(strCmd);

    strCmd = QString("nmcli connection up static_ip");
    strResult = executeLinuxCmd(strCmd);
    bool result=strResult.contains("successfully activated",Qt::CaseInsensitive);
    if(result == true)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool deleteStaticIp()
{
    QString strCmd = QString("nmcli con show --active|grep static");
    QString strResult = executeLinuxCmd(strCmd);
    QString strEthernetPort = strResult.section(" ",0,1);
    strCmd = QString("nmcli con del %1").arg(strEthernetPort);
    strResult = executeLinuxCmd(strCmd);

    bool result=strResult.contains("successfully deleted",Qt::CaseInsensitive);
    if(result == true)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool isStaticIpExist()
{
    QString isExistCmd = QString("nmcli con show --active|grep static");
    QString Result = executeLinuxCmd(isExistCmd);

    if(true == Result.isEmpty())
    {
        return false;
    }
    else
    {
        return true;
    }
}

QString getNetworkInfo(QStringList ethernetPort)
{
    if(!ethernetPort.isEmpty())
    {
        QString tmp;
        for(int i = 0;i<ethernetPort.size();i++)
        {
            QString strCmd = QString("ifconfig %1").arg(ethernetPort.at(i));
            QString retResult = executeLinuxCmd(strCmd);
            QStringList list;
            list = retResult.split("\n");
            for(int i = 0;i < list.count();i++)
            {
                retResult = list.at(i);
                tmp.append(retResult.trimmed());
                tmp.append("\n");
            }
        }
        return tmp;
    }
    return NULL;
}

QString getCurrentIp(QString name)
{
    QList<QNetworkInterface> network=QNetworkInterface::allInterfaces();
    foreach(QNetworkInterface net,network)
    {
        QString netName=net.humanReadableName();
        if(netName==name)
        {
            QList<QNetworkAddressEntry> list=net.addressEntries();
            foreach(QNetworkAddressEntry address,list)
            {
                if(address.ip().protocol()==QAbstractSocket::IPv4Protocol)
                {
                    return address.ip().toString();
                }
            }
        }
    }
    return 0;
}

void networkEnable(QStringList ethernetPort,bool flag)
{
    QString strCmd;
    QString strResult;
    if(!ethernetPort.isEmpty())
    {
        for(int i = 0;i<ethernetPort.size();i++)
        {
            if(flag == true)
            {
                strCmd = QString("ifconfig %1 up").arg(ethernetPort.at(i));
                strResult = executeLinuxCmd(strCmd);
            }
            else
            {
                strCmd = QString("ifconfig %1 down").arg(ethernetPort.at(i));
                strResult = executeLinuxCmd(strCmd);
            }
        }
    }
}

QString scanEthernetPort()
{
   QString strResult = executeLinuxCmd("nmcli device | grep 'eth'");
   return strResult;
}
